home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / IATHREAD.PAK / PRIORITY.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  12KB  |  403 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   priority.c
  9. //
  10. //  PURPOSE:   Displays the "Priority" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    CmdMCThreadPriority  - Displays the "About" dialog box
  14. //    PriorityDlgProc      - Processes messages for "Priority" dialog box.
  15. //    MsgPriorityInit      - To initialize the about box with version info
  16. //                           from resources.
  17. //    MsgPriorityCommand   - Process WM_COMMAND message sent to the about box.
  18. //    CmdPriorityOk        - Set priority/class user has selected.
  19. //    CmdPriorityCancel    - Clean-up actions if user cancels prioity dialog.
  20. //
  21. //  COMMENTS:
  22. //
  23. //
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #include <windowsx.h>
  27. #include "globals.h"            // prototypes specific to this application
  28. #include "resource.h"
  29.  
  30.  
  31. // Module-specific message- and command-handling functions
  32. LRESULT CALLBACK PriorityDlgProc(HWND, UINT, WPARAM, LPARAM);
  33. LRESULT MsgPriorityInit(HWND, UINT, WPARAM, LPARAM);
  34. LRESULT MsgPriorityCommand(HWND, UINT, WPARAM, LPARAM);
  35. LRESULT CmdPriorityOk(HWND, WORD, WORD, HWND);
  36. LRESULT CmdPriorityCancel(HWND, WORD, WORD, HWND);
  37.  
  38.  
  39. // Priority dialog message table definition.
  40. static MSD rgmsdPriority[] =
  41. {
  42.     {WM_COMMAND,    MsgPriorityCommand},
  43.     {WM_INITDIALOG, MsgPriorityInit}
  44. };
  45.  
  46. static MSDI msdiPriority =
  47. {
  48.     sizeof(rgmsdPriority) / sizeof(MSD),
  49.     rgmsdPriority,
  50.     edwpNone
  51. };
  52.  
  53. // Priority dialog command table definition.
  54. static CMD rgcmdPriority[] =
  55. {
  56.     {IDOK,     CmdPriorityOk},
  57.     {IDCANCEL, CmdPriorityCancel}
  58. };
  59.  
  60. static CMDI cmdiPriority =
  61. {
  62.     sizeof(rgcmdPriority) / sizeof(CMD),
  63.     rgcmdPriority,
  64.     edwpNone
  65. };
  66.  
  67.  
  68. // Priority class listbox data.
  69. static LISTBOXDATA rPriorityClassListData[] = {
  70.     { "Real Time", REALTIME_PRIORITY_CLASS },
  71.     { "High",      HIGH_PRIORITY_CLASS     },
  72.     { "Normal",    NORMAL_PRIORITY_CLASS   },
  73.     { "Idle",      IDLE_PRIORITY_CLASS     }
  74.     };
  75.  
  76.  
  77. // Thread priority listbox data.
  78. static LISTBOXDATA rThreadPriorityListData[] = {
  79.     { "Time Critical", THREAD_PRIORITY_TIME_CRITICAL },
  80.     { "Highest",       THREAD_PRIORITY_HIGHEST       },
  81.     { "Above Normal",  THREAD_PRIORITY_ABOVE_NORMAL  },
  82.     { "Normal",        THREAD_PRIORITY_NORMAL        },
  83.     { "Below Normal",  THREAD_PRIORITY_BELOW_NORMAL  },
  84.     { "Lowest",        THREAD_PRIORITY_LOWEST        },
  85.     { "Idle",          THREAD_PRIORITY_IDLE          }
  86.     };
  87.  
  88.  
  89. //
  90. //  FUNCTION: CmdMCThreadPriority(HWND, WORD, WORD, HWND)
  91. //
  92. //  PURPOSE: Displays the "Priority" dialog box
  93. //
  94. //  PARAMETERS:
  95. //    hwnd      - Window handle
  96. //    wCommand  - IDM_THREAD_PRIORITY (unused)
  97. //    wNotify   - Notification number (unused)
  98. //    hwndCtrl  - NULL (unused)
  99. //
  100. //  RETURN VALUE:
  101. //
  102. //    Always returns 0 - Message handled
  103. //
  104. //  COMMENTS:
  105. //    Call DialogBox() to display the priority dialog box.
  106.  
  107. #pragma argsused
  108. LRESULT CmdMCThreadPriority(HWND hwnd,
  109.                             WORD wCommand,
  110.                             WORD wNotify,
  111.                             HWND hwndCtrl
  112.                            )
  113. {
  114.     DialogBox(hInst,
  115.               MAKEINTRESOURCE(IDD_PRIORITY),
  116.               hwnd,
  117.               (DLGPROC)PriorityDlgProc);
  118.  
  119.     return 0;
  120. }
  121.  
  122.  
  123. //
  124. //  FUNCTION: PriorityDlgProc(HWND, UINT, WPARAM, LPARAM)
  125. //
  126. //  PURPOSE:  Processes messages for "Priority" dialog box.
  127. //
  128. //  PARAMETERS:
  129. //    hdlg     - window handle of the dialog box
  130. //    uMessage - type of message
  131. //    wparam   - message-specific information
  132. //    lparam   - message-specific information
  133. //
  134. //  RETURN VALUE:
  135. //    TRUE     - message handled
  136. //    FALSE    - message not handled
  137. //
  138. //  COMMENTS:
  139. //
  140.  
  141. LRESULT CALLBACK PriorityDlgProc(HWND   hdlg,
  142.                                  UINT   uMessage,
  143.                                  WPARAM wparam,
  144.                                  LPARAM lparam)
  145. {
  146.     return DispMessage(&msdiPriority, hdlg, uMessage, wparam, lparam);
  147. }
  148.  
  149.  
  150. //
  151. //  FUNCTION: MsgPriorityInit(HWND, UINT, WPARAM, LPARAM)
  152. ////  PURPOSE: To initialize the priority dialog box.
  153. //
  154. //  PARAMETERS:
  155. //    hwnd     - The window handing the message.
  156. //    uMessage - WM_INITDIALOG         (unused).
  157. //    wparam   - Message specific data (unused).
  158. //    lparam   - Message specific data (unused).
  159. //
  160. //  RETURN VALUE:
  161. //    Always returns 0 - message handled.
  162. //
  163. //  COMMENTS:
  164. //    Initializes Priority Class and Thread Priority list boxes before the
  165. //    dialog is displayed.
  166. //
  167.  
  168. #pragma argsused
  169. LRESULT MsgPriorityInit(HWND   hdlg,
  170.                         UINT   uMessage,
  171.                         WPARAM wparam,
  172.                         LPARAM lparam)
  173. {
  174.      DWORD  dwPriorityClass;
  175.      int    nPriority;
  176.      int    nIndex;
  177.      int    cItems;
  178.      int    i;
  179.  
  180.  
  181.      // Initialize priority class listbox and set the default priority
  182.     // class selection to the process's current priority class.
  183.  
  184.     cItems = sizeof(rPriorityClassListData) / sizeof(LISTBOXDATA);
  185.  
  186.     FillListBox(hdlg, 
  187.                 IDC_PRIORITY_CLASS_LIST, 
  188.                 rPriorityClassListData,
  189.                 cItems);
  190.  
  191.     dwPriorityClass = GetPriorityClass(GetCurrentProcess());
  192.  
  193.     for (i = 0; i < cItems; i++)
  194.           if (dwPriorityClass == (DWORD)rPriorityClassListData[i].dwData)
  195.             break;   //Found the priority class we're looking for.
  196.  
  197.     // This test should always fail because we should always have all of the
  198.     // different priority classes in the listbox.  In case a new priority
  199.     // is defined in a later version of the operating system, we want to know
  200.     // about it in the debugging version, and handle it gracefully in the
  201.     // release version.
  202. #ifdef _DEBUG
  203.     if (i >= cItems)
  204.         MessageBox(ghwndFrame, 
  205.                    "MsgPriorityInit(): Invalid priority class", 
  206.                    "Priority.c", 
  207.                          MB_OK);
  208. #else
  209.     if (i >= cItems)
  210.         i = 0;      // Set i to a reasonable value.
  211. #endif
  212.  
  213.     nIndex = SendDlgItemMessage(hdlg, 
  214.                                 IDC_PRIORITY_CLASS_LIST,
  215.                                 LB_FINDSTRINGEXACT, 
  216.                                 0,
  217.                                 (LPARAM)rPriorityClassListData[i].szString);
  218.     SendDlgItemMessage(hdlg, 
  219.                        IDC_PRIORITY_CLASS_LIST, 
  220.                               LB_SETCURSEL,
  221.                        nIndex, 
  222.                        0);
  223.  
  224.  
  225.     // Initialize the thread priority listbox and set the default thread
  226.     // priority selection to the MDI child window's thread's current
  227.     // priority.
  228.  
  229.     cItems = sizeof(rThreadPriorityListData) / sizeof(LISTBOXDATA);
  230.  
  231.     FillListBox(hdlg, 
  232.                 IDC_THREAD_PRIORITY_LIST, 
  233.                      rThreadPriorityListData,
  234.                 cItems);
  235.  
  236.     nPriority = GetThreadPriority(GetCurChildWindowThread());
  237.  
  238.     for (i = 0; i < cItems; i++)
  239.         if (nPriority == (int)rThreadPriorityListData[i].dwData)
  240.            break;   //Found the thread priority we're looking for.
  241.  
  242.     // This test should always fail because we should always have all of the
  243.     // different thread priorities in the listbox.  In case a new priority
  244.     // is defined in a later version of the operating system, we want to know
  245.     // about it in the debugging version, and handle it gracefully in the
  246.      // release version.
  247. #ifdef _DEBUG
  248.     if (i >= cItems)
  249.         MessageBox(ghwndFrame, 
  250.                    "MsgPriorityInit(): Invalid thread priority", 
  251.                    "Priority.c", 
  252.                    MB_OK);
  253. #else
  254.     if (i >= cItems)
  255.         i = 0;      // Set i to a reasonable value.
  256. #endif
  257.  
  258.     nIndex = SendDlgItemMessage(hdlg, 
  259.                                           IDC_THREAD_PRIORITY_LIST,
  260.                                 LB_FINDSTRINGEXACT, 
  261.                                 0,
  262.                                 (LPARAM)rThreadPriorityListData[i].szString);
  263.     SendDlgItemMessage(hdlg, 
  264.                        IDC_THREAD_PRIORITY_LIST, 
  265.                        LB_SETCURSEL,
  266.                        nIndex, 
  267.                        0);
  268.  
  269.  
  270.     // Center the dialog over the application window
  271.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  272.      return TRUE;
  273. }
  274.  
  275. //
  276. //  FUNCTION: MsgPriorityCommand(HWND, UINT, WPARAM, LPARAM)
  277. //
  278. //  PURPOSE: Process WM_COMMAND messages sent to the about box.
  279. //
  280. //  PARAMETERS:
  281. //    hwnd     - The window handing the message.
  282. //    uMessage - WM_COMMAND            (unused).
  283. //    wparam   - Message specific data (unused).
  284. //    lparam   - Message specific data (unused).
  285. //
  286. //  RETURN VALUE:
  287. //    Always returns 0 - message handled.
  288. //
  289. //  COMMENTS:
  290. //
  291.  
  292. #pragma argsused
  293. LRESULT MsgPriorityCommand(HWND   hwnd,
  294.                                     UINT   uMessage,
  295.                                     WPARAM wparam,
  296.                                     LPARAM lparam)
  297. {
  298.     return DispCommand(&cmdiPriority, hwnd, wparam, lparam);
  299. }
  300.  
  301. //
  302. //  FUNCTION: CmdPriorityOk(HWND, WORD, HWND)
  303. //
  304. //  PURPOSE: To set thread priority and priority class according to user's
  305. //           choice and then exit the dialog.
  306. //
  307. //  PARAMETERS:
  308. //    hwnd     - The window handling the command.
  309. //    wCommand - IDOK                 (unused).
  310. //    wNotify  - Notification number  (unused)
  311. //    hwndCtrl - NULL                 (unused).
  312. //
  313. //  RETURN VALUE:
  314. //    Always returns TRUE.
  315. //
  316. //  COMMENTS:
  317. //    Calls EndDialog to finish the dialog session.
  318. //
  319.  
  320. #pragma argsused
  321. LRESULT CmdPriorityOk(HWND hdlg,
  322.                              WORD wCommand,
  323.                       WORD wNotify,
  324.                       HWND hwndCtrl)
  325. {
  326.      int    nIndex;
  327.      int    nPriority;
  328.      HANDLE hThread;
  329.      DWORD  dwPriorityClass;
  330.  
  331.     // Set priority class and thread's priority based on what user has
  332.     // selected in the priority class and thread priority list boxes.
  333.     // If the user hasn't made a selection, then leave priority class
  334.     // and thread priority alone.
  335.  
  336.     nIndex = SendDlgItemMessage(hdlg, 
  337.                                IDC_PRIORITY_CLASS_LIST,
  338.                                LB_GETCURSEL, 
  339.                                0, 
  340.                                0);
  341.      if (nIndex != LB_ERR)
  342.         {
  343.         dwPriorityClass = SendDlgItemMessage(hdlg, 
  344.                                              IDC_PRIORITY_CLASS_LIST,
  345.                                              LB_GETITEMDATA, 
  346.                                              (WPARAM)nIndex,
  347.                                              0);
  348.         SetPriorityClass(GetCurrentProcess(), dwPriorityClass);
  349.         }
  350.  
  351.     nIndex = SendDlgItemMessage(hdlg, 
  352.                                 IDC_THREAD_PRIORITY_LIST,
  353.                                 LB_GETCURSEL, 
  354.                                           0,
  355.                                 0);
  356.     if (nIndex != LB_ERR)
  357.         {
  358.         hThread = GetCurChildWindowThread();
  359.         nPriority = (int)SendDlgItemMessage(hdlg, 
  360.                                             IDC_THREAD_PRIORITY_LIST,
  361.                                             LB_GETITEMDATA, 
  362.                                             (WPARAM)nIndex,
  363.                                             0);
  364.  
  365.         SetThreadPriority(hThread, nPriority);
  366.         }
  367.  
  368.     EndDialog(hdlg, TRUE);          // Exit the dialog
  369.     return TRUE;
  370. }
  371.  
  372.  
  373. //
  374. //  FUNCTION: CmdPriorityCancel(HWND, WORD, HWND)
  375. //
  376. //  PURPOSE: Exit the dialog box and cancel any selection user might have
  377. //           made.
  378. //
  379. //  PARAMETERS:
  380. //    hwnd     - The window handling the command.
  381. //    wCommand - IDCANCEL             (unused).
  382. //    wNotify  - Notification number  (unused)
  383. //    hwndCtrl - NULL                 (unused).
  384. //
  385. //  RETURN VALUE:
  386. //    Always returns TRUE.
  387. //
  388. //  COMMENTS:
  389. //    Calls EndDialog to finish the dialog session.
  390. //
  391.  
  392. #pragma argsused
  393. LRESULT CmdPriorityCancel(HWND hdlg,
  394.                                   WORD wCommand,
  395.                                   WORD wNotify,
  396.                                   HWND hwndCtrl)
  397. {
  398.      // Leave thread's priority and class alone.
  399.  
  400.     EndDialog(hdlg, FALSE);          // Exit the dialog
  401.     return TRUE;
  402. }
  403.